#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <wchar.h>
#include <time.h>
#include "note.h"
#include "note_add.c"
#include "note_out.c"
void put_all_note(){
    Note_by_time *list=time_head->next;
    if(list->next==NULL)
        wprintf(L"无笔记！\n");
    while(list->next!=NULL){
        put_one_note(list->note);
        list = list->next;
    }
    return;
}
void add_new_note(){//写得像坨米田共，等加了GUI之类的再改
    time_node->note=(Note*)malloc(sizeof(Note));
    Note *new=time_node->note;
    wprintf(L"标题:");
    wscanf(L"%ls",new->title);
    read_tag(new);
    time_t tim=time(NULL);
    struct tm *ti=(struct tm*)malloc(sizeof(struct tm));
    ti=localtime(&tim);
    new->date=ti->tm_mday+(ti->tm_mon+1)*100+(ti->tm_year+1900)*10000;
    new->time=(ti->tm_hour)*100+ti->tm_min;
    wprintf(L"请输入 %ls 字符串来结束输入\n笔记",OVER);
    read_note(new->note,stdin);
    time_node->next=(Note_by_time*)malloc(sizeof(Note_by_time));
    time_node=time_node->next;
    time_node->next=NULL;
    wprintf(L"添加成功\n\n");
    put_one_note(new);
    note_number++;
    system("cls");
}
void search_by_tag(){
    wchar_t tag[50];
    wprintf(L"请输入想查找的标签名:");
    wscanf(L"%ls",tag);
    Tag_list *tag_node=tag_head->tag_next;
    while(wcscmp(tag,tag_node->tag)){
        if(tag_node->tag_next==NULL){
            wprintf(L"未检测到标签 %s\n",tag);
            return ;
        }
        tag_node=tag_node->tag_next;
    }
    Note_list *Note=tag_node->note_list;
    wprintf(L"搜索标签名#%s# ,结果如下:");
    do{
        put_one_note(Note->note);
    }while((Note = Note->next)!=NULL);
    return ;
}
void save_all(){
    freopen("note.txt","w",stdout);
    wprintf(L"%d ",note_number);
    Note_by_time *li=time_head->next;
    while(li->next!=NULL){
        Note *note=li->note;
        wprintf(L"%s ", note->title);
        for(int i=0;i<note->tag_number;i++)
            wprintf(L"%ls ", note->tag[i]);
        wprintf(L"$ %d/%d/%d ", note->date / 10000, note->date / 100 % 100, note->date % 100);
        wprintf(L"%d:%02d", note->time / 100, note->time % 100);
        wprintf(L"%ls", note->note);
        wprintf(OVER);
        wprintf(L" ");
        li=li->next;
    }
    return ;
}
void delete_note(){
    wchar_t title[500];
    wprintf(L"请输入你想删除的笔记的标题:");
    wscanf(L"%ls",title);
    int cnt=0;
    Note_by_time *node=time_head;
    if(node->next==NULL){
        wprintf(L"删除失败,未找到该笔记\n");
        return ;
    }
    while(node->next!=NULL){
        Note_by_time *next=node->next;
        if(wcscmp(title,next->note->title)==0){
            Note *des=next->note;
            for(int i=0;i<des->tag_number;i++)
                delete_note_in_tag(des->tag[i],des);
            node->next=next->next;
            free(des),free(next);
            cnt++;
        }
        node=node->next;
    }
    if(!cnt)
        wprintf(L"删除失败,未找到该笔记");
    else wprintf(L"成功删除%d条笔记\n",cnt),note_number-=cnt;
    return ;
}
void output_tags(){
    Tag_list *list=tag_head->tag_next;
    while(list!=NULL){
        wprintf(L"#%ls#\n",list->tag);
        list=list->tag_next;
    }
    return;
}